package com.jshx.sz.gdjt.activity;

import java.util.ArrayList;
import java.util.HashMap;

import android.database.DataSetObserver;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import com.jshx.sz.gdjt.R;
import com.jshx.sz.gdjt.constant.ApplicationGlobal;
import com.jshx.sz.gdjt.db.DataBaseUtil;
/**
 * 结论Fragment
 * @author xiangchangan
 * @time 2013-08-22
 */
public class ResaultFrame extends Fragment 
{
	//合格选项
	private RadioButton radio1;
	
	//不合格选项
	private RadioButton radio2;
	
	//互检人下拉框
	private Spinner spinner;

	/**
	 * 工具数据容器
	 */
	private ArrayList<HashMap<String, String>> detailData = new ArrayList<HashMap<String, String>>();
	
	//页面数据
	private HashMap<String, String> data;
	
	//
	private String teamId;
	
	//单选组
	private RadioGroup resualt_radiogroup;
	
	//
	private static String YES = "1";
	
	//
	private static String NO = "0";
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.resualt_fragment, null);
		findView(view);
		initView();
		return view;
	}

	/**
	 * 组件实例化
	 * @param view
	 */
	private void findView(View view) {
		radio1 = (RadioButton)view.findViewById(R.id.resualt_ok);
		radio2 = (RadioButton)view.findViewById(R.id.resualt_notok);
		spinner = (Spinner)view.findViewById(R.id.resualt_spinner);
		resualt_radiogroup = (RadioGroup)view.findViewById(R.id.resualt_radiogroup);
	}
	/**
	 * 组件数据初始化
	 */
	private void initView() {
		String type = ((TaskActivityGroup)getActivity()).getType();
		if(type != null && (type.equals("checkHis") || type.equals("examineHis")))
		{
			spinner.setSelected(false);
		}
		String userId =ApplicationGlobal.userId;//用户编号
		//网络查询互检人
//		PersonThread thread=new PersonThread(handler, userId, detailData);
//		thread.start();
		
		//本地查询互检人
//		detailData = DataBaseUtil.queryCheckUserData(teamId);
		try {
			teamId = ((TaskActivityGroup)getActivity()).getTeamId();
			DataBaseUtil dao=new DataBaseUtil();
			detailData = dao.getCheckUserData(teamId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		spinner.setAdapter(adapter) ;
		data = ((TaskActivityGroup)getActivity()).getDataFragment5();
	}
	
	@Override
	public void onResume() {
		initViewData();
		super.onResume();
	}
	
	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) 
		{
			int what = msg.what;
			super.handleMessage(msg);
		 
			spinner.setAdapter(adapter) ;
		}
	};
	
	SpinnerAdapter adapter=new SpinnerAdapter() {
		
		@Override
		public void unregisterDataSetObserver(DataSetObserver observer) {
			
		}
		
		@Override
		public void registerDataSetObserver(DataSetObserver observer) {
			
		}
		
		@Override
		public boolean isEmpty() {
			return false;
		}
		
		@Override
		public boolean hasStableIds() {
			return false;
		}
		
		@Override
		public int getViewTypeCount() {
			return 0;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			TextView text = new TextView(ResaultFrame.this.getActivity());
			text.setTextColor(getResources().getColor(R.color.black));
			text.setPadding(10, 5, 0, 5);
			text.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
			text.setTextSize(20);
			HashMap map = (HashMap)detailData.get(position);
            text.setText(map.get(DataBaseUtil.user_table_clos[1]).toString());
            return text;
		}
		
		@Override
		public int getItemViewType(int position) {
			return 0;
		}
		
		@Override
		public long getItemId(int position) {
			return position;
		}
		
		@Override
		public Object getItem(int position) {
			return detailData.get(position);
		}
		
		@Override
		public int getCount() {
			return detailData.size();
		}
		
		@Override
		public View getDropDownView(int position, View convertView, ViewGroup parent) {
			TextView text =new TextView(ResaultFrame.this.getActivity());
			text.setTextColor(getResources().getColor(R.color.black));
			text.setPadding(10, 5, 0, 5);
			text.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
			text.setTextSize(20);
			HashMap map=(HashMap)detailData.get(position);
            text.setText(map.get(DataBaseUtil.user_table_clos[1]).toString());
            return text;
		}
	};

	public String getTeamId() {
		return teamId;
	}

	public void setTeamId(String teamId) {
		this.teamId = teamId;
	}

	/**
	 * 获得页面数据
	 * @return
	 */
	public HashMap<String, String> getData() {
		if(data == null)
		{
			data = new HashMap<String, String>();
		}
		int id = resualt_radiogroup.getCheckedRadioButtonId();
		if(id == R.id.resualt_ok)
		{
			data.put(DataBaseUtil.task_detail_type2_table_clos[37], YES);
		}
		else if(id == R.id.resualt_notok)
		{
			data.put(DataBaseUtil.task_detail_type2_table_clos[37], NO);
		}
		if(detailData != null && spinner.getSelectedItemPosition() < detailData.size())
		{
			HashMap<String, String> map = detailData.get(spinner.getSelectedItemPosition());
			data.put(DataBaseUtil.task_detail_type2_table_clos[38], map.get(DataBaseUtil.user_table_clos[1]));
			data.put(DataBaseUtil.task_detail_type2_table_clos[39], map.get(DataBaseUtil.user_table_clos[0]));
		}
		return data;
	}

	/**
	 * 设置页面数据
	 * @param data
	 */
	public void initViewData() {
		if(data != null)
		{
			if(data.get(DataBaseUtil.task_detail_type2_table_clos[37]) != null)
			{
				if(data.get(DataBaseUtil.task_detail_type2_table_clos[37]).equals(YES))
				{
					radio1.setChecked(true);
				}
				else if(data.get(DataBaseUtil.task_detail_type2_table_clos[37]).equals(NO))
				{
					radio2.setChecked(true);
				}
			}
			if(data.get(DataBaseUtil.task_detail_type2_table_clos[39]) != null)
			{
				if(detailData != null && detailData.size() > 0)
				{
					for (int i = 0; i < detailData.size(); i++) {
						if(detailData.get(i).get(DataBaseUtil.user_table_clos[0]) != null
								&& detailData.get(i).get(DataBaseUtil.user_table_clos[0])
								.equals(data.get(DataBaseUtil.task_detail_type2_table_clos[39])))
						{
							spinner.setSelection(i);
							break;
						}
					}
				}
			}
		}
	}
}
